package com.dkd.applet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dkd.applet.common.constant.OrderStatus;
import com.dkd.applet.common.constant.PayStatus;
import com.dkd.applet.domain.Channel;
import com.dkd.applet.domain.Order;
import com.dkd.applet.domain.Policy;
import com.dkd.applet.domain.VendingMachine;
import com.dkd.applet.domain.dto.PayDto;
import com.dkd.applet.mapper.OrderMapper;
import com.dkd.applet.service.ChannelService;
import com.dkd.applet.service.OrderService;
import com.dkd.applet.service.PolicyService;
import com.dkd.applet.service.VendingMachineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
@Autowired
    ChannelService channelService;
@Autowired
    VendingMachineService vendingMachineService;
@Autowired
    PolicyService policyService;
/**
     * 订单创建
     * @param payDto
     * @param platform
     * @return
     */
    @Override
    public Order createOrder(PayDto payDto, String platform) {
        //1.查询货道是否存在
        Channel channel = channelService.getChanneleByInnerCodeAndSku(payDto.getInnerCode(), payDto.getSkuId());
if (channel==null){
    throw new RuntimeException("货道不存在");
}
        //2.查询设备是否存在
        VendingMachine vm = vendingMachineService.getVendingMachineByInnerCode(payDto.getInnerCode());
        if (vm == null) {
            throw new RuntimeException("设备不存在");
        }
        //3. 封装订单对象
        Order order = new Order();
        order.setSkuId(channel.getSkuId());// 商品ID
        order.setSkuName(channel.getSku().getSkuName());// 商品名称
        order.setChannelCode(channel.getChannelCode());// 货道编号
        order.setOpenId(payDto.getOpenId());// 小程序端的openId
        order.setPayType(platform);// 封装平台名称
        order.setPayStatus(PayStatus.PAY_STATUS_NOPAY);// 支付状态：未支付
        order.setStatus(OrderStatus.ORDER_STATUS_CREATE);// 订单状态：创建
        order.setOrderNo(payDto.getInnerCode() + System.nanoTime());// 订单编号
        order.setInnerCode(vm.getInnerCode());// 设备编号
        order.setNodeId(vm.getNodeId());// 点位ID
        order.setRegionId(vm.getRegionId());// 区域ID
        order.setPartnerId(vm.getPartnerId());// 合作商id
        order.setBusinessType(vm.getBusinessType());// 商圈类型
        order.setAddr(vm.getAddr());// 设备地址
        order.setCreateTime(LocalDateTime.now());
        order.setPrice(channel.getSku().getPrice());// 商品价格
        order.setAmount(channel.getSku().getPrice());// 商品金额

         // 3-2计算售价金额
        // 获取自动售货机对应的策略
        Policy policy = policyService.getById(vm.getPolicyId());
        // 如果策略存在，则使用策略中的折扣率
        if (policy != null) {
            BigDecimal discount = new BigDecimal(policy.getDiscount());// 折扣率
            BigDecimal price = new BigDecimal(channel.getSku().getPrice());// 原价价格
            // 计算真实价格公式： (原价 * 折扣率) / 100
            int realPrice = price.multiply(discount).divide(new BigDecimal(100), 0, RoundingMode.HALF_UP).intValue();
            order.setAmount(realPrice);
        }

        order.setAmount(1);// todo 支付金额，开发期间，暂时写死为1分钱
        //4，保存订单
        this.save(order);
        return order;
    }

    /**
     * 根据订单编号查询订单
     * @param orderNo
     * @return
     */
    @Override
    public Order getByOrderNo(String orderNo) {
        QueryWrapper<Order> qw = new QueryWrapper<>();
        qw.lambda().eq(Order::getOrderNo, orderNo);
        return this.getOne(qw);
    }
}
